package com.jiepos.mpos.biz.channel.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.channel.service.HostTermService;
import com.jiepos.mpos.biz.channel.service.InfChannelService;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.common.entity.App_Host_TermEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 主机终端控制器</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.controller 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月29日下午3:43:21
 * @history（历次修订内容、修订人、修订时间等）
 */
@Controller
@RequestMapping("hostterm")
public class HostTermController extends BaseController {
	
	@Autowired
	private HostTermService hostTermService;
	
	@Autowired
	private InfChannelService infChannelService;
	
	
	/**
	 * 进入主机终端列表页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	@RequestMapping("/getHostTermList.do")
	public ModelAndView getHostTermList(HttpServletRequest request,HttpServletResponse response,Pagination pagination){
		ModelAndView model=new ModelAndView("/module/channel/hostterm/hostterm_list");
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 进入未上报主机终端列表页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	@RequestMapping("/getUnreportedHostTermList.do")
	public ModelAndView getUnreportedHostTermList(HttpServletRequest request,HttpServletResponse response,Pagination pagination){
		ModelAndView model=new ModelAndView("/module/channel/hostterm/unreporthostterm_list");
		return super.getDetail(request, response, model);
	}
	
	
	/**
	 * 查询主机终端信息json数据
	 * @param request
	 * @param pagination
	 * @param channel_no
	 * @param hostTerm
	 * @param ins_cd
	 * @param ins_cn_nm
	 * @return
	 */
	@RequestMapping("/getHostTermListJson.do")
	public @ResponseBody Map getHostTermListJson(HttpServletRequest request,Pagination pagination,String channel_no,App_Host_TermEntity hostTerm,
			String ins_cd,String ins_cn_nm,String rec_crt_ts_start,String rec_crt_ts_end){
		if(StringUtils.isBlank(channel_no)){
			return new HashMap();
		}
		Map channel=infChannelService.getTableNameByParam(channel_no);
		if(channel==null || channel.size()<1){
			return new HashMap();
		}
		String tableName=(String)channel.get("chn_prmtb");
		if(StringUtils.isBlank(tableName)){
			return new HashMap();
		}
		return hostTermService.getHostTermList(request,pagination, hostTerm, ins_cd, ins_cn_nm, tableName, channel_no,rec_crt_ts_start,rec_crt_ts_end);		 
	}
	
	/**
	 * 查询未上报主机终端信息json数据
	 * @param request
	 * @param pagination
	 * @param channel_no
	 * @param hostTerm
	 * @param ins_cd
	 * @param ins_cn_nm
	 * @return
	 */
	@RequestMapping("/getUnreportedHostTermListJson.do")
	public @ResponseBody Map getUnreportedHostTermListJson(HttpServletRequest request,Pagination pagination,String channel_no,
			App_Custom_InfEntity customerInfo){
		if(StringUtils.isBlank(channel_no)){
			return new HashMap();
		}
		Map channel=infChannelService.getTableNameByParam(channel_no);
		if(channel==null || channel.size()<1){
			return new HashMap();
		}
		String tableName=(String)channel.get("chn_prmtb");
		if(StringUtils.isBlank(tableName)){
			return new HashMap();
		}
		return hostTermService.getUnreportedHostTermList(request, pagination, customerInfo, tableName, channel_no);		 
	}
	
	/**
	 * 进入主机终端信息详细页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @param host_term_uuid
	 * @param channel_no
	 * @return
	 */
	@RequestMapping("/getHostTermDetail.do")
	public ModelAndView getHostTermDetail(HttpServletRequest request,HttpServletResponse response,Pagination pagination,
			String host_term_uuid,String channel_no){
		ModelAndView model=new ModelAndView("/module/channel/hostterm/hostterm_detail");
		Map channel=infChannelService.getTableNameByParam(channel_no);	
		String tableName=String.valueOf(channel.get("chn_prmtb"));	
		Map hostTerm=hostTermService.getHostTermDetail(host_term_uuid, tableName, channel_no);
		model.addObject("hostTerm", hostTerm);
		model.addObject("channel_no", channel.get("channel_no"));
		model.addObject("chnname", channel.get("chnname"));
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 保存主机终端
	 * @param request
	 * @param response
	 * @param hostTerm
	 * @param channel_no
	 * @return
	 */
	@RequestMapping("/saveHostTerm.do")
	public ModelAndView saveHostTerm(HttpServletRequest request,HttpServletResponse response,App_Host_TermEntity hostTerm,
			String channel_no){
		Map channel=infChannelService.getTableNameByParam(channel_no);	
		String tableName=String.valueOf(channel.get("chn_prmtb"));
		hostTermService.saveHostTerm(hostTerm, super.getLoginUser(request, response), tableName,channel_no);		
		Map map=super.getLoginUser(request, response);  
		return new ModelAndView(new RedirectView("getHostTermList.do"), map);
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param host_term_uuids
	 * @param channel_no
	 * @return
	 */
	@RequestMapping("/deleteHostTermByIds.do")
	public ModelAndView deleteHostTermByIds(HttpServletRequest request,HttpServletResponse response,String host_term_uuids,
			String channel_no){
		Map channel=infChannelService.getTableNameByParam(channel_no);	
		String tableName=String.valueOf(channel.get("chn_prmtb"));
		hostTermService.deleteHostTermByIds(host_term_uuids, tableName);		
		Map map=super.getLoginUser(request, response);  
		return new ModelAndView(new RedirectView("getHostTermList.do"), map);
	}
	
	/**
	 * 校验一机一码
	 * @param hostTerm
	 * @param channel_no
	 * @return
	 */
	@RequestMapping("/checkHostTerm.do")
	public @ResponseBody String checkHostLarge(App_Host_TermEntity hostTerm,String channel_no){	
		Map channel=infChannelService.getTableNameByParam(channel_no);			
		String tableName=String.valueOf(channel.get("chn_prmtb"));
		return hostTermService.checkHostTerm(hostTerm, tableName);
	}
	
	/**
	 * 导入
	 * @param request
	 * @param response
	 * @param channel_no
	 * @param insertFile
	 * @return
	 */
	@RequestMapping("/importHostTerm.do")
	public @ResponseBody String importHostTerm(HttpServletRequest request,HttpServletResponse response,String channel_no,
			@RequestParam(value = "insertFile",required = false) MultipartFile insertFile){			
		if(StringUtils.isBlank(channel_no)){			
			return "chnno";
		}
		if(insertFile.isEmpty()||insertFile.getSize()==0){			
			return "file";
		}
		Map channel=infChannelService.getTableNameByParam(channel_no);			
		String tableName=String.valueOf(channel.get("chn_prmtb"));		
		Map map=super.getLoginUser(request, response); 
		return hostTermService.importOrUpdateHostTerm(tableName,insertFile,map,"1");
	}
	
	/**
	 * 批量修改
	 * @param request
	 * @param response
	 * @param channel_no
	 * @param updateFile
	 * @return
	 */
	@RequestMapping("/updateHostTerm.do")
	public @ResponseBody String updateHostTerm(HttpServletRequest request,HttpServletResponse response,String channel_no,
			@RequestParam(value = "updateFile",required = false) MultipartFile updateFile){			
		if(StringUtils.isBlank(channel_no)){			
			return "chnno";
		}
		if(updateFile.isEmpty()||updateFile.getSize()==0){		
			return "file";
		}
		Map channel=infChannelService.getTableNameByParam(channel_no);			
		String tableName=String.valueOf(channel.get("chn_prmtb"));		
		Map map=super.getLoginUser(request, response); 
		return hostTermService.importOrUpdateHostTerm(tableName,updateFile,map,"2");
	}
	
	/**
	 * 下载模板
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportTemplate.do")
	@ResponseBody
	public void exportTemplate(HttpServletRequest request, HttpServletResponse response){
		super.clearCookie(request, response);
		String fileName = "yijiyimamuban.xls";
		String msg=hostTermService.exportTemplate(fileName, request, response);
		if (StringUtils.isBlank(msg)) {
			super.setFileParam(request, response, fileName);
		} else {
			super.setErrorFileParam(request, response, msg);
		}
	}
	
	/**
	 * 批量进件
	 * @param request
	 * @param response
	 * @param channel_no
	 * @return
	 */
	@RequestMapping("/reportToChannel.do")	
	public @ResponseBody String reportToChannel(HttpServletRequest request, HttpServletResponse response,String channel_no){
		return hostTermService.intoBatch(channel_no, super.getLoginUser(request, response));
	}
	
	/**
	 * 单个进件
	 * @param request
	 * @param response
	 * @param channelNo
	 * @param customerNo
	 * @return
	 */
	@RequestMapping("/intoSingle.do")	
	public @ResponseBody String intoSingle(HttpServletRequest request, HttpServletResponse response, String channelNo, String customerNo){
		return hostTermService.intoSingle(channelNo, customerNo, super.getLoginUser(request, response));
	}
	
	/**
	 * 批量设置扣率
	 * @param request
	 * @param response
	 * @param channel_no
	 * @param ins_cd
	 * @return
	 */
	@RequestMapping("/modifyRateToChannel.do")	
	public @ResponseBody String modifyRateToChannel(HttpServletRequest request, HttpServletResponse response,String channel_no,
			String ins_cd){		
		Map map=super.getLoginUser(request, response); 		
		return hostTermService.modifyRateToChannel(channel_no,ins_cd,map);
	}
	/**
	 * 修改结算信息
	 * @param request
	 * @param response
	 * @param channel_no
	 * @param host_term_uuid
	 * @return
	 */
	@RequestMapping("/modifyAccountToChannel.do")	
	public @ResponseBody String modifyAccountToChannel(HttpServletRequest request, HttpServletResponse response,String channel_no,
			String host_term_uuid){		
		Map map=super.getLoginUser(request, response); 		
		return hostTermService.modifyAccountToChannel(channel_no,host_term_uuid,map);
	}

}
